#include "label_show_hint.h"


#include <QtMath>
#include <QScreen>
#include <QGuiApplication>
#include <QPainter>


owid::Label_Show_Hint::Label_Show_Hint(QWidget *parent):
    QLabel(parent),
    shadow_width(10)
{
    // 信息提示框 顶层展示
    this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
    // true：自动换行
    this->setWordWrap(true);
    // 对齐方式 左上
    this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    // 设置 label 外边距：文本内容到边框的距离（以像素为单位
    this->setMargin(15);
    // 信息提示框 无边框展示
    this->setWindowFlags(Qt::FramelessWindowHint);
}

void owid::Label_Show_Hint::set_HintLab_txt(const QString &str,const QFont &font)
{
    this->setText(str);
    this->setFont(font);

    // 标签大小自适应于当前文本内容与字体
    this->adjustSize();
}

void owid::Label_Show_Hint::show_HintLab(const QPoint &show_base)
{
    const QSize &Windows_Size =  QGuiApplication::primaryScreen()->size();
    const QPoint &Forecast_Distance = QPoint(show_base.x() + this->width(),
                                             show_base.y() + this->height());
    QPoint show_Plase = show_base;

    if(Forecast_Distance.x() > Windows_Size.width()){
        show_Plase.setX(  show_base.x() - (Forecast_Distance.x() - Windows_Size.width())) ;
    }

    if(Forecast_Distance.y() > Windows_Size.height()){
        show_Plase.setY(  show_base.y() - (Forecast_Distance.y() - Windows_Size.height())) ;
    }

    this -> move(show_Plase);
    this -> show();
}

void owid::Label_Show_Hint::paintEvent(QPaintEvent *event)
{


    // 1. 初始化一个QPainter对象用来绘制
    // 2. QPainter::Antialiasing，让引擎尽可能的消除锯齿
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor color;
    int flag = 255/shadow_width;

    // 此处循环次数为阴影宽度
    for(size_t i=0; i < this->shadow_width; i++)
    {
        // 5. 添加一个矩形
        QRectF rect = QRectF(this->shadow_width-i, this->shadow_width-i,
                             this->width()-(this->shadow_width-i)*2, this->height()-(this->shadow_width-i)*2);


        int color_ladder = flag*i;
        color.setRgb(color_ladder,color_ladder,color_ladder);
        painter.setPen(color);



        // 7. 绘制线路
        painter.drawRect(rect);
        painter.drawRect(rect);
    }

    QLabel::paintEvent(event);

}
